package JDBC_TB;

import java.sql.*;
import java.util.ResourceBundle;

//JDBC连接Mysql的工具类
public class Jdbc_Tb {
    private Jdbc_Tb() {
    }

    static {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static Connection getConnection(String database, boolean autoCommit) throws SQLException {
        ResourceBundle bundle = ResourceBundle.getBundle("jdbcToll");
        String url = bundle.getString("url");
        String user = bundle.getString("user");
        String password = bundle.getString("password");
        Connection conn = DriverManager.getConnection(url + database + "?&useSSL=false", user, password);
        conn.setAutoCommit(autoCommit);

        return conn;
    }

    public static void closeSource(Connection conn, Statement stmt, ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
                System.out.println("查询结果集对象未关闭");
            }
        }
        if (stmt != null) {
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
                System.out.println("数据库操作对象未关闭");
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
                System.out.println("连接对象未关闭");
            }
        }


    }
}
